package com.gzxx.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.gzxx.constant.ServiceCodeStatus;
import com.gzxx.domain.base.BaseDto;
import com.gzxx.domain.base.BaseVo;
import com.gzxx.domain.converter.SysUserConverter;
import com.gzxx.domain.dto.SysUserCreateDto;
import com.gzxx.domain.dto.SysUserQueryDto;
import com.gzxx.domain.dto.SysUserUpdateDto;
import com.gzxx.domain.vo.SysUserVo;
import com.gzxx.service.SysUserService;
import com.gzxx.util.ServerResponse;
import com.gzxx.util.SysLoginUserHelper;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

/**
 * @author MAX
 * @date 2020/7/23 9:57 上午
 * 系统用户路由
 */

@Api(tags = "系统用户相关接口")
@RestController
@RequestMapping("users")
@RequiredArgsConstructor(onConstructor = @__(@Autowired))
public class SysUserController {

    private final SysUserService sysUserService;

    @ApiOperation("分页查询用户信息")
    @PostMapping("/list")
    @PreAuthorize("@rbac.hasPermission('system:users:list')")
    public ServerResponse<SysUserVo> list(@RequestBody SysUserQueryDto sysUserQueryDto) {
        if (ObjectUtils.isEmpty(sysUserQueryDto)) {
            sysUserQueryDto = new SysUserQueryDto();
        }
        IPage<SysUserVo> page = sysUserService.users(SysUserConverter.MAPPER.convertQueryDto(sysUserQueryDto));
        return ServerResponse.success(page);
    }

    @ApiOperation("根据用户id查询明细")
    @GetMapping("/detail")
    @PreAuthorize("@rbac.hasPermission('system:users:select')")
    public ServerResponse<SysUserVo> detail(@RequestParam("id") Long id) {
        SysUserVo user = sysUserService.singleDetail(id);
        return ServerResponse.success(user);
    }

    @ApiOperation("新增用户信息")
    @PostMapping("/add")
    @PreAuthorize("@rbac.hasPermission('system:users:add')")
    public ServerResponse add(@Valid @RequestBody SysUserCreateDto sysUserCreateDto) {
        return sysUserService.add(SysUserConverter.MAPPER.convertCreateDto(sysUserCreateDto));

    }

    @ApiOperation("更新用户信息")
    @PostMapping("/update")
    @PreAuthorize("@rbac.hasPermission('system:users:update')")
    public ServerResponse update(@Valid @RequestBody SysUserUpdateDto sysUserUpdateDto) {
        boolean result = sysUserService.update(SysUserConverter.MAPPER.convertUpdateDto(sysUserUpdateDto));
        return result ? ServerResponse.success(ServiceCodeStatus.SUCCESS) : ServerResponse.error();
    }

    @ApiOperation("初始化用户密码")
    @GetMapping("/init")
    @PreAuthorize("@rbac.hasPermission('system:users:update:initpwd')")
    public ServerResponse initPassword(@RequestParam("id") Long id) {
        boolean result = sysUserService.initPwd(SysUserConverter.MAPPER.convertById(id));
        return result ? ServerResponse.success(ServiceCodeStatus.SUCCESS) : ServerResponse.error();
    }

    @ApiOperation("变更用户状态")
    @PostMapping("/status")
    @PreAuthorize("@rbac.hasPermission('system:users:update:status')")
    public ServerResponse status(@Valid @RequestBody BaseDto baseDto) {
        boolean result = sysUserService.changeStatus(SysUserConverter.MAPPER.convertBaseDto(baseDto));
        return result ? ServerResponse.success(ServiceCodeStatus.SUCCESS) : ServerResponse.error();
    }

    @ApiOperation("获取个人信息")
    @GetMapping("/myInfo")
    public ServerResponse<SysUserVo> myInfo() {
        Long id = SysLoginUserHelper.getLoginUserId();
        SysUserVo user = sysUserService.singleDetail(id);
        return ServerResponse.success(user);
    }

    @ApiOperation("获取所有用户")
    @GetMapping("/types")
    public ServerResponse<BaseVo> type() {
        List<BaseVo> baseVos = sysUserService.getAll();
        return ServerResponse.success(baseVos);
    }
}
